package com.yehun.shiro;

import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.apache.shiro.authc.HostAuthenticationToken;
import org.apache.shiro.authc.RememberMeAuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;

@Data
public class UsernamePasswordTypeToken implements HostAuthenticationToken, RememberMeAuthenticationToken {

    private String username;
    private char[] password;
    private boolean rememberMe = false;
    private String host;
    private ShiroUserType type;

    public UsernamePasswordTypeToken() { }


    public UsernamePasswordTypeToken(final String username, final char[] password) {
        this(username, password, false, null);
    }


    public UsernamePasswordTypeToken(final String username, final String password) {
        this(username, password != null ? password.toCharArray() : null, false, null);
    }


    public UsernamePasswordTypeToken(final String username, final char[] password, final String host) {
        this(username, password, false, host);
    }


    public UsernamePasswordTypeToken(final String username, final String password, final String host) {
        this(username, password != null ? password.toCharArray() : null, false, host);
    }


    public UsernamePasswordTypeToken(final String username, final char[] password, final boolean rememberMe) {
        this(username, password, rememberMe, null);
    }


    public UsernamePasswordTypeToken(final String username, final String password, final boolean rememberMe) {
        this(username, password != null ? password.toCharArray() : null, rememberMe, null);
    }


    public UsernamePasswordTypeToken(final String username, final char[] password, final boolean rememberMe, final String host) {
        this.username = username;
        this.password = password;
        this.rememberMe = rememberMe;
        this.host = host;
    }


    public void clear() {
        this.username = null;
        this.host = null;
        this.rememberMe = false;

        if (this.password != null) {
            for (int i = 0; i < password.length; i++) {
                this.password[i] = 0x00;
            }
            this.password = null;
        }

    }

    @Override
    public Object getPrincipal() {
        return this.getUsername();
    }

    @Override
    public Object getCredentials() {
        return getPassword();
    }
}
